package com.weifly.egp.service.stock;

import com.weifly.egp.service.config.GridConfigService;
import com.weifly.egp.service.monitor.StockInfo;
import com.weifly.egp.service.monitor.StockMonitorService;
import com.weifly.egp.service.trade.SendOrderInfo;
import com.weifly.egp.service.trade.StockOrderInfo;
import com.weifly.egp.service.trade.StockTradeService;

import java.util.List;

/**
 * 股票更新上下文实现
 *
 * Created by weijianxin on 2018/10/31.
 */
public class StockUpdateContextImpl implements StockUpdateContext {

    private StockUpdateService stockUpdateService;
    private StockTradeService stockTradeService;
    private StockMonitorService stockMonitorService;
    private GridConfigService gridConfigService;

    private List<StockOrderInfo> orderList; // 已加载的委托单列表

    public void setStockUpdateService(StockUpdateService stockUpdateService) {
        this.stockUpdateService = stockUpdateService;
    }

    public void setStockTradeService(StockTradeService stockTradeService) {
        this.stockTradeService = stockTradeService;
    }

    public void setStockMonitorService(StockMonitorService stockMonitorService) {
        this.stockMonitorService = stockMonitorService;
    }

    public GridConfigService getGridConfigService() {
        return gridConfigService;
    }

    public void setGridConfigService(GridConfigService gridConfigService) {
        this.gridConfigService = gridConfigService;
    }

    public StockPrice getStockPrice(String stockCode) {
        return this.stockUpdateService.getStockPrice(stockCode);
    }

    public List<StockOrderInfo> getOrderList() {
        if(this.orderList==null){
            this.orderList = this.stockTradeService.getOrderList();
        }
        return this.orderList;
    }

    public StockOrderInfo sendOrder(SendOrderInfo sendOrderInfo) {
        return this.stockTradeService.sendOrder(sendOrderInfo);
    }

    public void saveConfig() {
        List<StockInfo> stockList = this.stockMonitorService.getStockList();
        this.gridConfigService.saveConfig(stockList);
    }

    public void fireStockListChange() {
        this.stockMonitorService.fireStockListChange();
    }
}
